<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
  >
    <el-table :data="formData" class="-mt-10px">
      <el-table-column label="序号" type="index" width="100" />
       <el-table-column label="纳税人识别号" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.taxationTaxid`" :rules="formRules.taxationTaxid" class="mb-0px!">
            <el-input v-model="row.taxationTaxid" placeholder="请输入纳税人识别号" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="实缴(退)金额(元)" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.taxationAmount`" :rules="formRules.taxationAmount" class="mb-0px!">
            <el-input v-model="row.taxationAmount" placeholder="请输入实缴(退)金额(元)" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="税种" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.taxationType`" :rules="formRules.taxationType" class="mb-0px!">
            <el-input v-model="row.taxationType" placeholder="请输入税种" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="品目名称" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.taxationName`" :rules="formRules.taxationName" class="mb-0px!">
            <el-input v-model="row.taxationName" placeholder="请输入品目名称" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="税务机关" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.taxationAuthority`" :rules="formRules.taxationAuthority" class="mb-0px!">
            <el-input v-model="row.taxationAuthority" placeholder="请输入税务机关" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="税款所属开始日期" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.taxationBegintime`" :rules="formRules.taxationBegintime" class="mb-0px!">
            <el-date-picker
              v-model="row.taxationBegintime"
              type="date"
              value-format="x"
              placeholder="选择税款所属开始日期"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="税款所属结束日期" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.taxationEndtime`" :rules="formRules.taxationEndtime" class="mb-0px!">
            <el-date-picker
              v-model="row.taxationEndtime"
              type="date"
              value-format="x"
              placeholder="选择税款所属结束日期"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="入(退)库日期" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.taxationTime`" :rules="formRules.taxationTime" class="mb-0px!">
            <el-date-picker
              v-model="row.taxationTime"
              type="date"
              value-format="x"
              placeholder="选择入(退)库日期"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="备注" min-width="200">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.remark`" :rules="formRules.remark" class="mb-0px!">
            <el-input v-model="row.remark" type="textarea" placeholder="请输入备注" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="状态" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.status`" :rules="formRules.status" class="mb-0px!">
            <el-select v-model="row.status" placeholder="请选择状态">
                <el-option
                  v-for="dict in getStrDictOptions(DICT_TYPE.SYS_STATUS)"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button type="danger" @click="handleDelete($index)" link>删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row justify="center" class="mt-3">
    <el-button @click="handleAdd" round>+ 添加租金税费管理</el-button>
  </el-row>
</template>
<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { RentApi } from '@/api/zszx/rent'

const props = defineProps<{
  rentId: undefined // 合同序号（主表的关联字段）
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref([])
const formRules = reactive({
  rentId: [{ required: true, message: '合同序号不能为空', trigger: 'blur' }],
  taxationAmount: [{ required: true, message: '实缴(退)金额(元)不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.rentId,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return;
    }
    try {
      formLoading.value = true
      formData.value = await RentApi.getTaxationListByRentId(val)
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    id: undefined,
    rentId: undefined,
    taxationTaxid: undefined,
    taxationAmount: undefined,
    taxationType: undefined,
    taxationName: undefined,
    taxationAuthority: undefined,
    taxationBegintime: undefined,
    taxationEndtime: undefined,
    taxationTime: undefined,
    remark: undefined,
    status: undefined,
  }
  row.rentId = props.rentId
  formData.value.push(row)
}

/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}

/** 表单值 */
const getData = () => {
  return formData.value
}

defineExpose({ validate, getData })
</script>